<template>
  <div>
    <el-dialog :title="title" :closeOnClickModal="false" :visible.sync="show" width="650px">
      <el-form label-position="right" :model="selectTable" ref="addForm" label-width="80px" :inline="false" autocomplete="off">
        <el-form-item label="用户帐号" prop="userAccount" :rules="[rule[0],{ min: 3, message: '最少输入3个字符', trigger: 'blur'}]">
          <el-input placeholder="请输入帐号" v-model="selectTable.userAccount" maxLength="20" style="width:200px" :disabled="selectTable.method==='edit'"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="userName" :rules="[rule[0],{ min: 1, message: '最少输入1个字符', trigger: 'blur'}]">
          <el-input placeholder="请输入名称" v-model="selectTable.userName" maxLength="20" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="userStatus" :rules="[rule[0]]">
          <el-switch :disabled="selectTable.userId < 2" v-model="selectTable.userStatus" :inactive-value="1" :active-value="0" active-text="启用" inactive-text="停用">
          </el-switch>
        </el-form-item>
        <template v-if="selectTable.method==='edit'">
          <el-form-item label="密码" prop="password" :rules="[{ min: 5, max: 20, message: '最少输入5个字符', trigger: 'change' }]">
            <el-input placeholder="请输入密码" v-model="selectTable.password" maxLength="20" type="password" style="width:150px"></el-input>
            <span>可不填密码，不填则不修改密码</span>
          </el-form-item>
          <el-form-item label="重复密码" prop="password2" :rules="[{ min: 5, max: 20, message: '最少输入5个字符', trigger: 'change' },rule[1]]">
            <el-input placeholder="请重复输入密码" v-model="selectTable.password2" maxLength="20" type="password" style="width:150px"></el-input>
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="密码" prop="password" :rules="[rule[0],{ min: 5, max: 20, message: '最少输入5个字符', trigger: 'change' }]">
            <el-input placeholder="请输入密码" v-model="selectTable.password" maxLength="20" type="password" style="width:150px"></el-input>
          </el-form-item>
          <el-form-item label="重复密码" prop="password2" :rules="[rule[0],{ min: 5, max: 20, message: '最少输入5个字符', trigger: 'change' },rule[1]]">
            <el-input placeholder="请重复输入密码" v-model="selectTable.password2" maxLength="20" type="password" style="width:150px"></el-input>
          </el-form-item>
        </template>
        <el-form-item label="备注" prop="comment">
          <el-input placeholder="请输入备注" v-model="selectTable.comment" maxLength="50" style="width:450px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userSave, userModify } from '@/utils/api'
export default {
  components: {
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.data.authUser || {}
    this.selectTable.method = this.data.method
    this.selectTable.password2 = ''
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value !== this.selectTable.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' },
        { validator: validatePass, trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        if (this.selectTable.method === 'edit') {
          await userModify(this.selectTable)
        } else if (this.selectTable.method === 'add') {
          await userSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>